capture log close
clear all
set more off
pause off
# delimit;

local output "";
local log "";
local data "";

/* 
Figure A3.3B
*/

use "`data'\collapse_combine_byearnavgcat.dta", clear;


/* ******************************* */
/* **** DATA PREP AND RESHAPE **** */
/* ******************************* */

/* Age */
gen age=year-ser_dobyy;
replace age=99 if year==9999;
order ser_dobyy year age;

/* Create <=1937 and >=1943 cohorts */
tab ser_dobyy, m;
gen ser_dobcoh=1937 if ser_dobyy>=1935 & ser_dobyy<=1937;
replace ser_dobcoh=1943 if ser_dobyy>=1943 & ser_dobyy<=1945;
replace ser_dobcoh=9999 if ser_dobcoh==.;
order ser_dobyy ser_dobcoh year age;


/* Sum over month-age and YOB cohort */
collapse (sum) earn10yr_* earn55_*, by(age ser_dobcoh n_earnavgcat);
sort ser_dobcoh age n_earnavgcat;

/* Create fractions of cohorts instead of counts (for flow variable, use single event denominator) */
foreach set in earn10yr earn55 {;
foreach stub in `set'_lfexit_flow `set'_lfexit_one `set'_lfexit_one_1 `set'_lfexit_one_rbst `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany {;
	egen total_`stub'=total(`stub'), by(ser_dobcoh n_earnavgcat);
};
};
foreach set in earn10yr earn55 {;
foreach stub in `set'_lfexit_one `set'_lfexit_one_1 `set'_lfexit_one_rbst `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany {;	
	rename `stub' ct_`stub';
	gen `stub'=ct_`stub'/total_`stub' if ct_`stub'>10;
	replace `stub'=. if ct_`stub'<=10;
};
	rename `set'_lfexit_flow ct_`set'_lfexit_flow;
	gen `set'_lfexit_flow=ct_`set'_lfexit_flow/total_`set'_lfexit_one;
	replace `set'_lfexit_flow=. if ct_`set'_lfexit_flow<=10;
};
drop total_* ct_*;

/* Reshape for plot */
drop if age<50;
egen id=group(age n_earnavgcat);
reshape wide 
	earn10yr_lfexit_flow earn10yr_lfexit_one earn10yr_lfexit_one_1 earn10yr_lfexit_one_rbst
	earn10yr_claim_phusold earn10yr_claim_phusany earn10yr_claim_mbaold earn10yr_claim_mbaany
	earn55_lfexit_flow earn55_lfexit_one earn55_lfexit_one_1 earn55_lfexit_one_rbst
	earn55_claim_phusold earn55_claim_phusany earn55_claim_mbaold earn55_claim_mbaany
, i(id) j(ser_dobcoh);
order id age n_earnavgcat;


/* ***************************************************** */
/* **** PLOT RETIREMENT AND CLAIMING ACROSS COHORTS **** */
/* ***************************************************** */

keep if age>=55 & age<69 ;

local cond_lfexit_one_1		"age>=55 & age<69" ;

foreach set in earn10yr {;
foreach stub in lfexit_one_1 {;
twoway 	
	connected `set'_`stub'1937 age if n_earnavgcat==0, lcolor(orange*.5) lwidth(medthick) lpattern(solid) mcolor(orange*.5) msymbol(S) ||
	connected `set'_`stub'1943 age if n_earnavgcat==0 & `cond_`stub'', lcolor(green*.5) lwidth(medthick) lpattern(shortdash) mcolor(green*.5) msymbol(Sh) ||
	connected `set'_`stub'1937 age if n_earnavgcat==1, lcolor(orange*.75) lwidth(medthick) lpattern(solid) mcolor(orange*.75) msymbol(T) ||
	connected `set'_`stub'1943 age if n_earnavgcat==1 & `cond_`stub'', lcolor(green*.75) lwidth(medthick) lpattern(shortdash) mcolor(green*.75) msymbol(Th) ||
	connected `set'_`stub'1937 age if n_earnavgcat==2, lcolor(orange*1.25) lwidth(medthick) lpattern(solid) mcolor(orange*1.25) msymbol(X) ||
	connected `set'_`stub'1943 age if n_earnavgcat==2 & `cond_`stub'', lcolor(green*1.25) lwidth(medthick) lpattern(shortdash) mcolor(green*1.25) msymbol(X) ||
	connected `set'_`stub'1937 age if n_earnavgcat==3, lcolor(orange*2) lwidth(medthick) lpattern(solid) mcolor(orange*2) msymbol(O) ||
	connected `set'_`stub'1943 age if n_earnavgcat==3 & `cond_`stub'', lcolor(green*2) lwidth(medthick) lpattern(shortdash) mcolor(green*2) msymbol(Oh)
	xtitle("Age", size(medsmall)) xsc(r(55 68)) xlab(55(1)68)
	ytitle("Fraction of birth cohort", size(medsmall))
	legend(col(2) row(4) label(1 "Q1, FRA 65") label(2 "Q1, FRA 66") label(3 "Q2, FRA 65") label(4 "Q2, FRA 66")
		label(5 "Q3, FRA 65") label(6 "Q3, FRA 66") label(7 "Q4, FRA 65") label(8 "Q4, FRA 66"))
	graphregion(color(white))
	bgcolor(white)
	ylabel(,nogrid)
	;
	graph export "`output'\\Figure_A33B.png", replace;
};
};
